# -*- coding: utf-8 -*-
"""
@Time ： 2024/9/25 11:11
@Auth ： 周瑞东 Ryan.Zhou
@File ：sub_t_monitoring_conn.py
"""
import subprocess
import time


from PyQt5.QtCore import QObject, pyqtSignal, QRunnable, QThread
from utils.global_state import GlobalState
from hal.hardware_manager import HardwareManager

class SignalsMonitoringConn(QObject):
    update_navi_time = pyqtSignal()
    data_air_quality = pyqtSignal(list)
    data_envir = pyqtSignal(list)



class ThreadMonitoringConn(QRunnable):
    def __init__(self):
        super(ThreadMonitoringConn, self).__init__()
        self.__global_state = GlobalState()
        self.is_paused = False
        self.is_killed = False
        self.is_running = False
        # self.ip_addr_des = ip_addr_des
        # self.ip_addr_src = ip_addr_src
        self.sigs = SignalsMonitoringConn()
        # self.__hal_serial = HALSerial()
        self.__hal = HardwareManager()

    def pause(self):
        self.is_paused = True

    def resume(self):
        self.is_paused = False

    def kill(self):
        self.is_killed = True

    def run(self):
        print("ThreadMonitoringConn 线程启动")
        self.is_running = True
        # while not self.__global_state.network.usb5631_is_connected:
        while True:
            while self.is_paused:
                time.sleep(0)

            if self.is_killed:
                break

            self.sigs.update_navi_time.emit()

            list_air_data = self.__hal.get_air_quality()
            if list_air_data is not None:
                self.sigs.data_air_quality.emit(list_air_data)

            list_envir_data = self.__hal.get_temp_humdi()
            if list_envir_data is not None:
                self.sigs.data_envir.emit(list_envir_data)
                
            
            #     self.__global_state.e1682.e1682_is_reachable_via_ping = True

            QThread.msleep(1000)  # 使用 QThread.msleep 延时 1 秒

        print("ThreadMonitoringConn 线程关闭")
        self.is_running = False